package cn.study.zgm.nettyall.channel;

import javafx.util.Builder;

import java.nio.ByteBuffer;

/**
 * packageName cn.study.zgm.nettyall.channel
 *
 * @author 你的名字
 * @version JDK 8
 * @className TestByteBufferExam (此处以class为例)
 * @date 2025/2/24
 * @description TODO
 */
public class TestByteBufferExam {
    public static void main(String[] args) {
        // 发送方连续发送得多个小得数据包
        final ByteBuffer buffer = ByteBuffer.allocate(32);
        buffer.put("chongqing\nnihao\nwe".getBytes());
        split(buffer);
        buffer.put("lcome".getBytes());
        split(buffer);
    }

    private static void split(ByteBuffer buffer) {
        // 切换为读模式
        buffer.flip();
        for (int i = 0; i < buffer.limit(); i++) {
            if (buffer.get(i)=='\n'){
                int length = i +1- buffer.position();
                byte[] bytes = new byte[length];
                buffer.get(bytes,0,length);
                ByteBuffer tempByteBuffer = ByteBuffer.wrap(bytes);
                ByteBufferUtil.debugAll(tempByteBuffer);
            }
        }
        // 切换为写模式
        buffer.compact();
    }
}
